package com.qn.wechat.service;

import com.jeesuite.common.json.JsonUtils;
import com.jeesuite.confcenter.ConfigChangeHanlder;
import com.jeesuite.mybatis.plugin.pagination.Page;
import com.jeesuite.spring.ApplicationStartedListener;
import com.jeesuite.springweb.model.WrapperResponse;
import com.oneplatform.base.exception.AssertUtil;
import com.oneplatform.base.exception.ExceptionCode;
import com.qn.wechat.common.MapperUtils;
import com.qn.wechat.dao.entity.QrchannelEntity;
import com.qn.wechat.dao.entity.ext.QrchannelEntityExt;
import com.qn.wechat.dao.mapper.QRChannelEntityMapper;
import com.qn.wechat.ifservice.dto.NewsArticleDto;
import com.qn.wechat.ifservice.dto.QRChannelContentDto;
import com.qn.wechat.ifservice.dto.QRChannelDto;
import com.qn.wechat.ifservice.dto.para.QRChannelQueryPara;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class QRChannelService implements ConfigChangeHanlder, ApplicationStartedListener {

    @Resource
    QRChannelEntityMapper qrChannelEntityMapper;
    @Resource
    MapperUtils mapperUtils;

    /**
     * 查询渠道列表
     */
    public Page<QRChannelDto> getList(QRChannelQueryPara para) {
        Page<QRChannelDto> result = new Page<>();
        List<QRChannelDto> dtos = null;

        Page<QrchannelEntityExt> list = qrChannelEntityMapper.findQrchannelList(para, para);
        if (list != null && list.getData() != null) {
            BeanUtils.copyProperties(list, result);
            dtos = mapperUtils.map(list.getData(), QRChannelDto.class);
        }
        if (dtos != null && dtos.size() > 0) {
            for (int i = 0; i < dtos.size(); i++) {
                QRChannelDto dto = dtos.get(i);
                //渠道内容
                QrchannelEntityExt ext = list.getData().get(i);
                if (StringUtils.isNotBlank(ext.getChannelcontent())) {

                    dto.setChannelcontentModel(JsonUtils.toObject(ext.getChannelcontent(), QRChannelContentDto.class));
                }
                //客服消息内容
                if (StringUtils.isNotBlank(ext.getNewsArticle())) {
                    dto.setNewsArticleModel(JsonUtils.toList(ext.getNewsArticle(), NewsArticleDto.class));
                }

            }
        }

        result.setData(dtos);
        return result;
    }


    /**
     * 新增或者修改渠道
     */
    @Transactional
    public WrapperResponse<String> save(QRChannelDto dto) {
        AssertUtil.isTrue(dto.getWid() != null && dto.getWid() != 0, ExceptionCode.REQUEST_PARAM_ERROR.code, "公众号ID不能为空");
        AssertUtil.notBlank(dto.getChannelname(), "渠道名称不能为空");

        WrapperResponse<String> response = new WrapperResponse<>();
        QrchannelEntity entity = mapperUtils.map(dto, QrchannelEntity.class);

        if (dto.getNewsArticleModel() != null && dto.getNewsArticleModel().size() > 0
                && StringUtils.isNotBlank(dto.getNewsArticleModel().get(0).getTitle())) {
            entity.setNewsArticle(JsonUtils.toJson(dto.getNewsArticleModel()));
        }
        entity.setChannelcontent(JsonUtils.toJson(dto.getChannelcontentModel()));
        //如果当前是启用，则停用所有
        if(entity.getStatus()==1){
            QrchannelEntity update = new QrchannelEntity();
            update.setWid(entity.getWid());
            update.setStatus(-1);
            update.setChanneltype(entity.getChanneltype());
            qrChannelEntityMapper.updateChannelStatus(update);
//            throw new RuntimeException("测试失误回滚");
        }

        if (entity.getId() == null || entity.getId() == 0) {
            int id = qrChannelEntityMapper.saveQrchannel(entity);
            response.setData(String.valueOf(id));
        } else {
            qrChannelEntityMapper.updateByPrimaryKey(entity);
            response.setData(String.valueOf(dto.getId()));
        }

        return response;
    }

    @Override
    public void onConfigChanged(Map<String, Object> map) {

    }

    @Override
    public void onApplicationStarted(ApplicationContext applicationContext) {

    }
}
